import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/login/index.dart';
import 'package:flutter_enjoy_plus/notice/index.dart';
import 'package:flutter_enjoy_plus/pages/tabs_page.dart';
import 'package:flutter_enjoy_plus/profile/index.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

void main() {
  runApp(MaterialApp(
    routes: {
      '/': (context) => const TabsPage(),
      '/notice_detail': (context) => const NoticeDetail(),
      "/login": (context) => const LoginPage(),
      // '/profile': (context) => const ProfilePage()
    },
    initialRoute: '/',
    //? 路由拦截-pushNamed 进行跳转时 && 访问的页面路径并不是上方routes中所定义的
    onGenerateRoute: (setting) {
      //? setting 路由对象 setting 路由的名称/setting.arguments 路由的参数
      //? 获取token 判断token是否存在
      final token = tokenManager.getToken();
      if (token.isEmpty) {
        return MaterialPageRoute(
          builder: (context) => const LoginPage(),
        );
      } else {
        if (setting.name == '/profile') {
          return MaterialPageRoute(builder: (context) => const ProfilePage());
        }
      }
    },
  ));
}
